﻿using System.Web.UI;
using System.Web.UI.WebControls;
using Ue.Framework.Web.WebControls;
using Ue.Infrastructure.Web.UI;
using Ue.Qcdz.Domain.Medias;
using Ue.Qcdz.Domain.Tags;

namespace Ue.Qcdz.UI.Controls.Medias
{
    public class MediasEdit : SkinnedEntityEdit<TheMedia>
    {
        private string skinFileName = "~/Admin/Themes/Skins/Medias/MediaEdits.ascx";
        private HiddenField hdfCategory;
        private HiddenField hdfTagKey;
        private TextBox tbxMediaName;
        private TextBox tbxMediaImage;
        private TextBox tbxMediaLink;

      

        #region Overrides of SkinnedControl
     
        protected override string GetSkinFileName()
        {
            return skinFileName;
        }

        #endregion

        #region Overrides of SkinnedEntityEdit<TheMedia>

        protected override void InitSkin(Control skin)
        {
            tbxMediaName = skin.FindControl<TextBox>("tbxMediaName");
            tbxMediaImage = skin.FindControl<TextBox>("tbxMediaImage");
            tbxMediaLink = skin.FindControl<TextBox>("tbxMediaLink");
            hdfCategory = skin.FindControl<HiddenField>("hdfCategory");
            hdfTagKey = skin.FindControl<HiddenField>("hdfTagKey");

        }

        protected override TheMedia Enttiy
        {
            get
            {
                if (this.Action == "Delete")
                {
                    return new TheMedia(this.EntityKey);
                }
                Tag tag = null;
                if (!string.IsNullOrEmpty(hdfCategory.Value))
                {
                    Domain.TreeViews.TreeNode node = MediaService.TagTreeView.FindNodeBy(hdfCategory.Value);
                    tag = TagService.GetTag(node.Tag.Key);
                }
                else
                {
                    tag = TagService.GetTag(hdfTagKey.Value);
                }
                TheMedia media = new TheMedia(
                    tbxMediaName.GetValue<string>(),
                    tbxMediaImage.GetValue<string>(),
                    tbxMediaLink.GetValue<string>(),

                    this.EntityKey
                    );
                media.MediaType = tag;
                return media;
              
            }
            set
            {
                if (value == null)
                {
                    return;
                }
                tbxMediaName.SetValue(value.MediaName);
                tbxMediaImage.SetValue(value.MediaImage);
                tbxMediaLink.SetValue(value.MediaLink);
                this.hdfTagKey.Value = value.MediaType.Key.ToString(); 
                
            }
        }

        #endregion
    }
}
